// pages/MovieIndex/movieDetail/movieDetail.js
const MovieHttp = require("../../../utils/MovieHttp")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        arrList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        var { url } = options;
        var urlArr = [":4000/api/movie/inTheaters", ":4000/api/movie/comingSoon", ":4000/api/movie/top250"];
        var httpRes = await MovieHttp.getHttp(url);
        if (url == urlArr[0]) {
            httpRes.data.res.forEach(item => {
                item.rating = item.raiting;
                item.rat = this.getRating(item.raiting);
            })
            this.setData({
                arrList: httpRes.data.res
            })
        } else if (url == urlArr[1]) {
            httpRes.data.res.forEach(item => {
                item.rat = this.getRating(item.rating);
            })
            this.setData({
                arrList: httpRes.data.res
            })
        } else if (url == urlArr[2]) {
            httpRes.data.res.forEach(item => {
                item.rat = this.getRating(item.rating);
            })
            this.setData({
                arrList: httpRes.data.res
            })
        }
    },
    getRating(num) {
        var arr = [];
        for (var i = 0; i < 5; i++) {
            if (num >= 2) {
                arr.push(2);
            } else if (num > 0) {
                arr.push(Math.round(num * 10) / 10)
            } else {
                arr.push(0)
            }
            num = num - 2;
        }
        return arr;
    },
    jumpDetail(e) {
        var { aid } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/MovieIndex/movieDetails/movieDetails?id=${aid}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})